package com.example.myone.Activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.myone.Base.ViewPagerAdapter;
import com.example.myone.Base.BasePager;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.MusicMapper;
import com.example.myone.Mapper.UserArticleLikeMapper;
import com.example.myone.Mapper.UserMapper;
import com.example.myone.Pager.HomePager;
import com.example.myone.Pager.DiscoverPager;
import com.example.myone.Pager.MyPager;
import com.example.myone.Pager.RadioPager;
import com.example.myone.R;
import com.google.android.material.bottomnavigation.BottomNavigationMenuView;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("NewApi")
public class MainActivity extends AppCompatActivity {

    private static BottomNavigationView bottomNavigationView;
    private MenuItem menuItem;
    private static ViewPager viewPager;
    private List<BasePager> pagerList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        initView();
        initData();
        disableShiftMode();
        bottomNavigationView.setOnItemSelectedListener(item -> {
            if (item.getItemId() == R.id.bottom_home) {
                viewPager.setCurrentItem(0);
            } else if (item.getItemId() == R.id.bottom_discover) {
                viewPager.setCurrentItem(1);
            } else if (item.getItemId() == R.id.bottom_recorder) {
                viewPager.setCurrentItem(2);
            } else if (item.getItemId() == R.id.bottom_my) {
                viewPager.setCurrentItem(3);
            } else {
                viewPager.setCurrentItem(0);
            }
            return false;
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    //如果有已选中的item，则取消它的的选中状态
                    menuItem.setChecked(false);
                } else {
                    //如果没有，则取消默认的选中状态（第一个item）
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                //让与当前Pager相应的item变为选中状态
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void initView() {
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        viewPager = findViewById(R.id.content_view_pager);
    }

    public void initData() {
        pagerList.add(new HomePager(this));
        pagerList.add(new DiscoverPager(this));
        pagerList.add(new RadioPager(this));
        pagerList.add(new MyPager(this, getSupportFragmentManager()));
        ViewPagerAdapter adapter = new ViewPagerAdapter(pagerList);
        viewPager.setAdapter(adapter);
        // 初始化数据库数据
        UserMapper.init(this);
        UserArticleLikeMapper.init(this);
        ArticleMapper.init(this);
        MusicMapper.init(this);
    }

    @SuppressLint("RestrictedApi")
    public static void disableShiftMode() {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
        menuView.setLabelVisibilityMode(NavigationBarView.LABEL_VISIBILITY_LABELED);
        bottomNavigationView.setItemActiveIndicatorEnabled(false);
        menuView.buildMenuView();
    }
}
